//
// Created by 11510 on 2024/7/19.
//

#ifndef DAY80_DEMO4_MOSQUITOWIDGET_H
#define DAY80_DEMO4_MOSQUITOWIDGET_H

#include <QWidget>
#include <QEvent>
#include <QMouseEvent>
#include <QPainter>
#include <QRandomGenerator>

class MosquitoWidget : public QWidget {
Q_OBJECT

public:
    explicit MosquitoWidget(QWidget *parent = nullptr) : QWidget(parent), score(0) {
        setFixedSize(600, 450);
        moveMosquito();
    }

    /**
     * 随机生成尸体的坐标
     */
    void moveMosquito() {
        int x = QRandomGenerator::global()->bounded(QWidget::width() - 50);
        int y = QRandomGenerator::global()->bounded(QWidget::height() - 50);
        mosquitoPos = QPoint(x, y);
        QWidget::update();
    }

    int getScore() const {
        return score;
    }

signals:
    void scoreChanged(int newScore);

protected:
    void paintEvent(QPaintEvent *event) override {
        QPainter painter(this);
        QPixmap pixmap(":/images/尸体.png");
        painter.drawPixmap(mosquitoPos.x(), mosquitoPos.y(), pixmap);
    }

    void mousePressEvent(QMouseEvent *event) override {
        /**
         * x,y为左上角顶点坐标
         * QRect(x, y, width, height)
         */
        if (QRect(mosquitoPos.x(), mosquitoPos.y(), 46, 55).contains(event->pos())) {
            score++;
            emit scoreChanged(score);
            moveMosquito();
        }
    }
private:
    QPoint mosquitoPos;
    int score;
};


#endif //DAY80_DEMO4_MOSQUITOWIDGET_H
